package demo2.util.ai;

import demo2.parser.BinaryTree;
import demo2.util.Evaluator;

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class AISearcher implements Searcher {

    public Set<String> searchAll(int[] arr) {
        Set<String> lists = new TreeSet<>();
        for (int cc = 0; cc < Canto.FAC[4]; cc++) {
            int[] idx = Canto.codel(cc, 4);
            String a = String.valueOf(arr[idx[0]]);
            String b = String.valueOf(arr[idx[1]]);
            String c = String.valueOf(arr[idx[2]]);
            String d = String.valueOf(arr[idx[3]]);
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    for (int k = 0; k < 4; k++) {
                        String X = OPERATORS[i];
                        String Y = OPERATORS[j];
                        String Z = OPERATORS[k];
                        for (String[] ee : new String[][]{{a, b, X, c, Y, d, Z}, {a, b, c, X, Y, d, Z},
                                {a, b, X, c, d, Y, Z}, {a, b, c, X, d, Y, Z}, {a, b, c, d, X, Y, Z},}) {
                            if (Math.abs(new Evaluator().eval(ee) - GOAL) < EPS) {
                                BinaryTree tree = BinaryTree.creatTree(Arrays.asList(ee));
                                StringBuffer buffer = new StringBuffer();
                                tree.midVisit(buffer);
                                lists.add(buffer.toString());
                            }
                        }
                    }
        }
        return lists;
    }

}
